package com.ruoyi.develop.goodsSaledo.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysArea;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsOrderIn.service.IGoodsOrderInService;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSale.service.IGoodsSaleService;
import com.ruoyi.develop.lvDo.domain.LvDo;
import com.ruoyi.develop.lvDo.service.ILvDoService;
import com.ruoyi.develop.orderIronRecord.domain.OrderIronRecord;
import com.ruoyi.develop.orderIronRecord.service.IOrderIronRecordService;
import com.ruoyi.develop.projectBaseInfo.domain.ProjectBaseInfo;
import com.ruoyi.develop.projectBaseInfo.service.IProjectBaseInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.goodsSaledo.domain.GoodsSaledo;
import com.ruoyi.develop.goodsSaledo.service.IGoodsSaledoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 请购单（自制/采购/外协）Controller
 * @author qh
 * @date 2024-08-09
 */
@RestController
@RequestMapping("/goodsSaledo/goodsSaledo")
public class GoodsSaledoController extends BaseController {
    @Autowired
    private IGoodsSaledoService goodsSaledoService;

    @Autowired
    private ILvDoService lvDoService;
    @Autowired
    private IProjectBaseInfoService projectBaseInfoService;
    @Autowired
    private IOrderIronRecordService orderIronRecordService;
    @Autowired
    private IGoodsOrderInService goodsOrderInService;
    @Autowired
    private IGoodsSaleService goodsSaleService;

    /**
     * 查询请购单（自制/采购/外协）列表
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledo:query')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsSaledo entity) {
        startPage();
        List<GoodsSaledo> list = goodsSaledoService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 查询请购单（自制/采购/外协）列表 剩余采购不为  0 的
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledo:query')")
    @GetMapping("/listBuyRemainNotZero")
    public TableDataInfo listBuyRemainNotZero(GoodsSaledo entity) {
        startPage();
        List<GoodsSaledo> list = goodsSaledoService.listBuyRemainNotZero(entity);
        return getDataTable(list);
    }

    /**
     * 查询请购单（自制/采购/外协）列表  产品合并
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledo:query')")
    @GetMapping("/listMerge")
    public TableDataInfo listMerge(GoodsSaledo entity) {
        startPage();
        List<GoodsSaledo> list = goodsSaledoService.selectListMerge(entity);
        return getDataTable(list);
    }


    //查询数量不是0的
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledo:query')")
    @GetMapping("/listQinggou")
    public TableDataInfo listQinggou(GoodsSaledo entity) {
        startPage();
        List<GoodsSaledo> list = goodsSaledoService.listQinggou(entity);
        return getDataTable(list);
    }

    //查询数量不是0的并且全是自制的
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledo:query')")
    @GetMapping("/listQinggouIsDo")
    public TableDataInfo listQinggouIsDo(GoodsSaledo entity) {
        startPage();
        List<GoodsSaledo> list = goodsSaledoService.listQinggouIsDo(entity);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledo:query')")
    @GetMapping("/listQinggouAll")
    public TableDataInfo listQinggouAll(GoodsSaledo entity) {
        startPage();
        List<GoodsSaledo> list = goodsSaledoService.listQinggouAll(entity);
        return getDataTable(list);
    }



    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledo:query')")
    @GetMapping("/listSuoding")
    public TableDataInfo listSuoding(GoodsSaledo entity) {
        startPage();
        List<GoodsSaledo> list = goodsSaledoService.listSuoding(entity);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledo:query')")
    @GetMapping("/listQinggouByBuyallId")
    public TableDataInfo listQinggouByBuyallId(GoodsSaledo entity) {
        startPage();
        List<GoodsSaledo> list = goodsSaledoService.listQinggouByBuyallId(entity);
        return getDataTable(list);
    }
    /**
     * 导出请购产品列表
     */
    @Log(title = "导出请购产品列表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void exportDetailList(HttpServletRequest request, HttpServletResponse response, GoodsSaledo entity) {
        goodsSaledoService.exportData(request,response,entity);
    }

    /**
     * 导出请购单产品列表
     */
    @Log(title = "导出请购单产品列表", businessType = BusinessType.EXPORT)
    @GetMapping("/exportByPleaseBuy")
    public void exportByPleaseBuy(HttpServletRequest request, HttpServletResponse response, GoodsSaledo entity) {
        goodsSaledoService.exportDataByPleaseBuy(request,response,entity);
    }

    /**
     * 导出多个请购单产品列表
     */
    @Log(title = "导出请购单产品列表", businessType = BusinessType.EXPORT)
    @GetMapping("/exportByPleaseBuyIds")
    public void exportByPleaseBuyIds(HttpServletRequest request, HttpServletResponse response, GoodsSaledo entity) {
        goodsSaledoService.exportByPleaseBuyIds(request,response,entity);
    }

    /**
     * 获取请购单（自制/采购/外协）详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(goodsSaledoService.selectById(id));
    }


    @GetMapping("/listGoodsOrderInById/{id}")
    public AjaxResult listGoodsOrderInById(@PathVariable("id") String id) {
        return AjaxResult.success(goodsSaledoService.listGoodsOrderInById(id));
    }
    /**
     * 新增请购单（自制/采购/外协）
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledo:add')")
    @Log(title = "请购单（自制/采购/外协）", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody GoodsSaledo entity) {
        return toAjax(goodsSaledoService.insert(entity));
    }

    /**
     * 修改请购单（自制/采购/外协）
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledo:edit')")
    @Log(title = "请购单（自制/采购/外协）", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody GoodsSaledo entity) {
        return toAjax(goodsSaledoService.update(entity));
    }

    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledo:edit')")
    @Log(title = "技术修改请购单", businessType = BusinessType.UPDATE)
    @PutMapping("/editAndGoodsSale")
    public AjaxResult editAndGoodsSale(@RequestBody GoodsSaledo entity) {
        return toAjax(goodsSaledoService.editAndGoodsSale(entity));
    }

    @PutMapping("/editJishu")
    public AjaxResult editJishu(@RequestBody GoodsSaledo entity) {
        return toAjax(goodsSaledoService.updateJishu(entity));
    }
    /**
     * 删除请购单（自制/采购/外协）
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledo:remove')")
    @Log(title = "请购单（自制/采购/外协）", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(goodsSaledoService.deleteByIds(ids));
    }

    /**
     * 删除请购单（自制/采购/外协）
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledo:remove')")
    @Log(title = "删除二次请购订单产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeTwiceBuyGoods/{ids}")
    public AjaxResult removeTwiceBuyGoods(@PathVariable String[] ids) {
        return toAjax(goodsSaledoService.deleteTwiceBuyGoodsByIds(ids));
    }

    @PostMapping("/pleaseBuyForPrj")
    public AjaxResult pleaseBuyForPrj(@Validated @RequestBody GoodsSaledo entity) {
        String message=goodsSaledoService.insertBatch(entity);
        if(StringUtils.isNotBlank(message)){
            return AjaxResult.error(message);
        }else{
            return AjaxResult.success("提交完成！");
        }
    }

    @PostMapping("/pleaseBuyForPrjKuguan")
    public AjaxResult pleaseBuyForPrjKuguan(@Validated @RequestBody GoodsSaledo entity) {
        LvDo lvDo = lvDoService.selectById(entity.getLvDo().getId());
        if(!BusinessConstants.PLEASE_BUY_STATUS_STORE_SUBMIT.equals(lvDo.getStatus())){
            return AjaxResult.error("已弃审至库管，请联系后重新提交！");
        }
        String message=goodsSaledoService.insertBatch(entity);
        if(StringUtils.isNotBlank(message)){
            return AjaxResult.error(message);
        }else{
            return AjaxResult.success("提交完成！");
        }
    }
    //库管提交到生产部
    @PostMapping("/pleaseBuyForPrjNew")
    public AjaxResult pleaseBuyForPrjNew(@Validated @RequestBody GoodsSaledo entity) {
        ProjectBaseInfo projectBaseInfo=projectBaseInfoService.selectById(entity.getProjectId());
        if(BusinessConstants.PROJECT_1.equals(projectBaseInfo.getStatus())){
            goodsSaledoService.insertBatchNew(entity, projectBaseInfo);
            /*projectBaseInfo.setStatus(BusinessConstants.PROJECT_2);
            projectBaseInfoService.update(projectBaseInfo);*/
            projectBaseInfoService.updateStatus(BusinessConstants.PROJECT_2,projectBaseInfo.getId());
            return AjaxResult.success("提交完成！");
        }else {
            return AjaxResult.error("该订单已申请请购，请核验！");
        }
    }

    @PostMapping("/pleaseBuyForPrjNewToCaigou")
    public AjaxResult pleaseBuyForPrjNewToCaigou(@Validated @RequestBody GoodsSaledo entity) {
        if((BusinessConstants.SALE_TYPE_1).equals(entity.getProStatus())){
            ProjectBaseInfo projectBaseInfo=projectBaseInfoService.selectById(entity.getProjectId());
            if(projectBaseInfo!=null) {
                goodsSaledoService.insertBatchNewToCaigou(entity,projectBaseInfo);
               /* projectBaseInfo.setStatus(BusinessConstants.PROJECT_4);
                projectBaseInfoService.updateBackMoney(projectBaseInfo);*/
                projectBaseInfoService.updateStatus(BusinessConstants.PROJECT_4,projectBaseInfo.getId());
            }
            return AjaxResult.success("提交完成！");
        }else {
            return AjaxResult.error("该订单已申请请购，请核验！");
        }
    }

    // 铁件请购 库管提交
    @PostMapping("/pleaseIronToBuy")
    public AjaxResult pleaseIronToBuy(@Validated @RequestBody GoodsSaledo entity) {
            LvDo lvDo = lvDoService.selectById(entity.getMainId());
            if(BusinessConstants.PLEASE_BUY_IRON_STATUS_STORE_SUBMIT.equals(lvDo.getIronStatus())){
                return AjaxResult.error("该订单库管已提交，请联系后重新提交！");
            }
            lvDo.setIronStatus(BusinessConstants.PLEASE_BUY_IRON_STATUS_STORE_SUBMIT);
            lvDoService.update(lvDo);
            List<GoodsSaledo> list =  entity.getColumns();
            for (GoodsSaledo goodsSaledo : list) {
                goodsSaledoService.update(goodsSaledo);
                // 更新库存 锁定 数量
                GoodsOrderIn goodsOrderIn = goodsOrderInService.selectById(goodsSaledo.getGoodsId());
                if (goodsOrderIn != null) {
                    goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().subtract(goodsSaledo.getLockCount()));
                    goodsOrderInService.update(goodsOrderIn);
                }

            }
            return AjaxResult.success("提交完成！");
    }

    /**
     * 材料请购
     */
    @PostMapping("/pleaseBuyForMaterial")
    public AjaxResult pleaseBuyForMaterial(@Validated @RequestBody GoodsSaledo entity) {

        String ironRecordId = entity.getIronRecordId();
        OrderIronRecord orderIronRecord = new OrderIronRecord();
        if(StringUtils.isNotBlank(ironRecordId)){
            orderIronRecord = orderIronRecordService.selectById(ironRecordId);
        }else{
            return AjaxResult.error("没有选择铁件记录，请核验！");
        }

        if(BusinessConstants.ORDER_IRON_STATE_MATERIAL.equals(orderIronRecord.getStatus()) ||
                BusinessConstants.ORDER_IRON_STATE_REJECT.equals(orderIronRecord.getStatus())){
            goodsSaledoService.pleaseBuyForMaterial(entity,orderIronRecord);
            orderIronRecord.setStatus(BusinessConstants.ORDER_IRON_STATE_BUY);
            orderIronRecordService.update(orderIronRecord);
            return AjaxResult.success("提交完成！");
        }else {
            return AjaxResult.error("该铁件记录已申请请购，请核验！");
        }
    }

    /**
     * 多个订单下的产品批量请购
     */
    @PostMapping("/pleaseBuyForMultiOrders")
    public AjaxResult pleaseBuyForMultiOrders(@Validated @RequestBody GoodsSaledo entity) {
//        if(BusinessConstants.SALE_TYPE_1.equals(entity.getProStatus())){
            List<ProjectBaseInfo> projectBaseInfoList = new ArrayList<>();
            String[] orderIds = entity.getParams().get("orderIds").toString().split(",");
            for(String orderId:orderIds) {
                ProjectBaseInfo projectBaseInfo = projectBaseInfoService.selectById(orderId);
                if(!projectBaseInfo.getStatus().equals(BusinessConstants.PROJECT_1)){
                    return AjaxResult.error("订单("+projectBaseInfo.getCode()+")不是项目提交状态，请核验！");
                }
                projectBaseInfoList.add(projectBaseInfo);
            }
            List<GoodsSaledo>  goodsSaledoList = entity.getColumns();
            GoodsSaledo goodsSaledo = new GoodsSaledo();
            goodsSaledo.setColumns(goodsSaledoList);
            goodsSaledoService.insertForOrderMerge(goodsSaledo,projectBaseInfoList);

            for(ProjectBaseInfo projectBaseInfo:projectBaseInfoList){
                projectBaseInfoService.updateStatus(BusinessConstants.PROJECT_2,projectBaseInfo.getId());
            }
            return AjaxResult.success("提交完成！");
    }

    @PostMapping("/pleaseBuyForMultiOrdersToCaigou")
    public AjaxResult pleaseBuyForMultiOrdersToCaigou(@Validated @RequestBody GoodsSaledo entity) {
        List<ProjectBaseInfo> projectBaseInfoList = new ArrayList<>();
        String[] orderIds = entity.getParams().get("orderIds").toString().split(",");
        for(String orderId:orderIds) {
            ProjectBaseInfo projectBaseInfo = projectBaseInfoService.selectById(orderId);
            if(!projectBaseInfo.getStatus().equals(BusinessConstants.PROJECT_1)){
                return AjaxResult.error("订单("+projectBaseInfo.getCode()+")不是项目提交状态，请核验！");
            }
            projectBaseInfoList.add(projectBaseInfo);
        }
        List<GoodsSaledo>  goodsSaledoList = entity.getColumns();
        GoodsSaledo goodsSaledo = new GoodsSaledo();
        goodsSaledo.setColumns(goodsSaledoList);
        goodsSaledoService.insertForOrderMergeToCaigou(goodsSaledo,projectBaseInfoList);

        for(ProjectBaseInfo projectBaseInfo:projectBaseInfoList){
            projectBaseInfoService.updateStatus(BusinessConstants.PROJECT_4, projectBaseInfo.getId());
        }
        return AjaxResult.success("提交完成！");
    }

    @GetMapping("/listSaleProject")
    public TableDataInfo listSaleProject(GoodsSaledo entity) {
        startPage();
        List<GoodsSaledo> list = goodsSaledoService.listByProjectId(entity);
        return getDataTable(list);
    }

    @Log(title = "删除项目商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteProject/{ids}")
    public AjaxResult deleteProject(@PathVariable String[] ids) {
        return toAjax(goodsSaledoService.deletSaleInfoForPrj(ids[0]));
    }

    @Log(title = "提交其他产品的请购申请", businessType = BusinessType.UPDATE)
    @PostMapping("/submitScalado")
    public AjaxResult submitScalado(@Validated @RequestBody GoodsSaledo goodsSaledo) {
        goodsSaledoService.submitScalado(goodsSaledo);
        return AjaxResult.success("提交其他产品的请购申请完成！");
    }

    @Log(title = "提交其他产品的请购单修改", businessType = BusinessType.UPDATE)
    @PostMapping("/insertDoByLvdo")
    public AjaxResult insertDoByLvdo(@Validated @RequestBody GoodsSaledo goodsSaledo) {
        goodsSaledoService.insertDoByLvdo(goodsSaledo);
        return AjaxResult.success("提交其他产品的请购申请完成！");
    }

    @Log(title = "提交其他产品的驳回申请", businessType = BusinessType.UPDATE)
    @PostMapping("/insertDoByLvdoBohui")
    public AjaxResult insertDoByLvdoBohui(@Validated @RequestBody GoodsSaledo goodsSaledo) {
        goodsSaledoService.insertDoByLvdoBohui(goodsSaledo);
        return AjaxResult.success("提交其他产品的请购申请完成！");
    }


    @PostMapping("/insertBatchLvDo")
    public AjaxResult insertBatchLvDo(@Validated @RequestBody LvDo entity) {
        goodsSaledoService.insertBatchLvDo(entity);
        return AjaxResult.success("选择完成！");
    }


    // 铁件产品选择
    @PostMapping("/insertBatchIron")
    public AjaxResult insertBatchIron(@Validated @RequestBody LvDo entity) {
        goodsSaledoService.insertBatchIron(entity);
        return AjaxResult.success("选择完成！");
    }

    // 铁件产品选择
    @PutMapping("/updateIron")
    public AjaxResult updateBatchIron(@Validated @RequestBody LvDo entity) {
        goodsSaledoService.updateBatchIron(entity);
        return AjaxResult.success("选择完成！");
    }

    //库管单个产品的修改
    @PutMapping("/kuguanEdit")
    public AjaxResult kuguanEdit(@Validated @RequestBody GoodsSaledo entity) {
        if(entity.getId()!=null){
            LvDo lvDo=lvDoService.selectById(entity.getMainId());
            if(lvDo!=null){
                if(lvDo.getId()!=null){
                        GoodsSaledo goodsSaledo=goodsSaledoService.selectById(entity.getId());
                        GoodsSale goodsSale=goodsSaleService.selectById(entity.getGoodsSaleId());
                        if(goodsSale!=null){
                            if(goodsSale.getId()!=null){
                                goodsSale.setOrderCount(goodsSale.getOrderCount().subtract(entity.getLockCount()).add(goodsSaledo.getLockCount()));
                                goodsSale.setLockCount(goodsSale.getLockCount().subtract(entity.getLockCount()).add(goodsSaledo.getLockCount()));
                                goodsSaleService.update(goodsSale);
                                GoodsOrderIn goodsOrderIn= goodsOrderInService.selectByCode(entity.getGoodsCode());
                                if(goodsOrderIn!=null){
                                    goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().subtract(entity.getLockCount()).add(goodsSaledo.getLockCount()));
                                    goodsOrderInService.updateEntity(goodsOrderIn);
                                }
                                /*goodsSaledo.setLockCount(entity.getLockCount());
                                goodsSaledo.setPleaseBuyCount(entity.getPleaseBuyCount());
                                goodsSaledo.setGoodsBuyTotal(entity.getOrderCount().multiply(entity.getGoodsBuyPrice()));*/
                                goodsSaledoService.update(entity);
                            }else{
                                return AjaxResult.error("选择请购产品的在订单产品id不存在，请检查后在修改！");
                            }
                        }else{
                            return AjaxResult.error("选择请购产品的在订单产品中不存在，请检查后在修改！");
                        }
                }else{
                    return AjaxResult.error("选择产品的请购单id不存在，请检查后在修改！");
                }
            }else{
                return AjaxResult.error("选择产品的请购单不存在，请检查后在修改！");
            }
            return AjaxResult.success("选择完成！");
        }else{
            return AjaxResult.error("选择产品不存在，请刷新后修改！");
        }
    }

    //库管单个产品的退回给履约
    @PutMapping("/editToLvyue")
    public AjaxResult editToLvyue(@Validated @RequestBody GoodsSaledo entity) {
        if(entity.getId()!=null){
            LvDo lvDo=lvDoService.selectById(entity.getMainId());
            if(lvDo!=null){
                if(lvDo.getId()!=null){
                     // GoodsSaledo goodsSaledo=goodsSaledoService.selectById(entity.getId());
                        GoodsSale goodsSale=goodsSaleService.selectById(entity.getGoodsSaleId());
                        if(goodsSale!=null){
                            if(goodsSale.getId()!=null){
                                goodsSale.setPleaseBuyCount(goodsSale.getPleaseBuyCount().subtract(entity.getPleaseBuyCount()));
                                goodsSale.setLockCount(goodsSale.getLockCount().subtract(entity.getLockCount()));
                                goodsSale.setStatus(BusinessConstants.GOODS_30);
                                goodsSaleService.update(goodsSale);
                              //  goodsSaleService.deleteByIdThree(goodsSale.getId());//关闭产品
                                goodsSaledoService.deleteById(entity.getId());
                                GoodsOrderIn goodsOrderIn= goodsOrderInService.selectByCode(entity.getGoodsCode());
                                if(goodsOrderIn!=null){
                                    goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().add(entity.getLockCount()));
                                    goodsOrderInService.update(goodsOrderIn);
                                }
                            }else{
                                return AjaxResult.error("选择请购产品的在订单产品id不存在，请检查后在修改！");
                            }
                        }else{
                            return AjaxResult.error("选择请购产品的在订单产品中不存在，请检查后在修改！");
                        }
                }else{
                    return AjaxResult.error("选择产品的请购单id不存在，请检查后在修改！");
                }
            }else{
                return AjaxResult.error("选择产品的请购单不存在，请检查后在修改！");
            }
            return AjaxResult.success("退回完成！");
        }else{
            return AjaxResult.error("退回产品不存在，请刷新后修改！");
        }
    }

    @PostMapping("/submitLvdoToCg")
    public AjaxResult submitLvdoToCg(@Validated @RequestBody LvDo entity) {
        goodsSaledoService.submitLvdoToCg(entity);
        return AjaxResult.success("选择完成！");
    }


    @PostMapping("/submitLvdoToTech")
    public AjaxResult submitLvdoToTech(@Validated @RequestBody LvDo entity) {
        goodsSaledoService.submitLvdoToTech(entity);
        return AjaxResult.success("选择完成！");
    }


    @PostMapping("/submitLvdoToBuyDept")
    public AjaxResult submitLvdoToBuyDept(@Validated @RequestBody LvDo entity) {
        goodsSaledoService.submitLvdoToBuyDept(entity);
        return AjaxResult.success("操作成功！");
    }



    @Log(title = "更新其他产品的请购单或驳申请", businessType = BusinessType.UPDATE)
    @PostMapping("/updateLvdo")
    public AjaxResult updateLvdo(@Validated @RequestBody LvDo entity) {
        goodsSaledoService.updateLvdo(entity);
        return AjaxResult.success("提交其他产品的请购申请完成！");
    }

    @Log(title = "更新铁件产品的请购单或驳申请", businessType = BusinessType.UPDATE)
    @PostMapping("/updateLvdoIron")
    public AjaxResult updateLvdoIron(@Validated @RequestBody LvDo entity) {
        goodsSaledoService.updateLvdoIron(entity);
        return AjaxResult.success("提交其他产品的请购申请完成！");
    }

    //请购审批同意（郑主任）
    @GetMapping(value = "/subSuccess/{id}")
    public AjaxResult subSuccess(@PathVariable("id") String id) {
        goodsSaledoService.subSuccess(id);
        return AjaxResult.success("提交完成！");
    }

    //请购批款审批同意（郑主任）
    @GetMapping(value = "/tongyi/{ids}")
    public AjaxResult tongyi(@PathVariable("ids") String[] ids) {
        for(String id:ids){
            goodsSaledoService.subSuccess(id);
        }
        return AjaxResult.success("提交完成！");
    }

    //采购部审批同意（采购部）
    @GetMapping(value = "/subSuccessToCaigou/{id}")
    public AjaxResult subSuccessToCaigou(@PathVariable("id") String id) {
        goodsSaledoService.subSuccessToCaigou(id);
        return AjaxResult.success("提交完成！");
    }

    //请购批款审批同意（采购部）
    @GetMapping(value = "/tongyiCaigou/{ids}")
    public AjaxResult tongyiCaigou(@PathVariable("ids") String[] ids) {
        for(String id:ids){
            goodsSaledoService.subSuccessToCaigou(id);
        }
        return AjaxResult.success("提交完成！");
    }
    @Log(title = "采购单产品信息导入数据", businessType = BusinessType.INSERT)
    @PostMapping("/importCaigouData")
    public AjaxResult importCaigouData(GoodsSaledo entity, MultipartFile file) {
        Map<String, Object> error = new HashMap<>();
        error = goodsSaledoService.importCaigouData(entity,file);
        error.put("code", 200);
        return AjaxResult.success(error);
    }

    @Log(title = "采购单材料信息导入数据", businessType = BusinessType.INSERT)
    @PostMapping("/importCaigouCailiao")
    public AjaxResult importCaigouCailiao(GoodsSaledo entity, MultipartFile file) {
        Map<String, Object> error = new HashMap<>();
        error = goodsSaledoService.importCaigouCailiao(entity,file);
        error.put("code", 200);
        return AjaxResult.success(error);
    }

    @GetMapping("/listGoods")
    public TableDataInfo listGoods(GoodsSaledo entity) {
        List<GoodsSaledo> list = goodsSaledoService.listGoods(entity);
        for (GoodsSaledo goodsSaledo: list) {
            // 如请购单数量大于可用数量
            if (goodsSaledo.getPleaseBuyCount().compareTo(goodsSaledo.getRemainderCount()) >= 0) {
                goodsSaledo.setPleaseBuyCount(goodsSaledo.getPleaseBuyCount().subtract(goodsSaledo.getRemainderCount()));
                goodsSaledo.setLockCount(goodsSaledo.getRemainderCount());
            }else{

                goodsSaledo.setLockCount(goodsSaledo.getPleaseBuyCount());
                goodsSaledo.setPleaseBuyCount(BigDecimal.ZERO);
            }

        }
        return getDataTable(list);
    }

    //库管 二次请购 提交到 生产部
    @PostMapping("/pleaseBuyForTwiceToTech")
    public AjaxResult pleaseBuyForTwiceToTech(@Validated @RequestBody LvDo entity) {
        List<GoodsSaledo> rows = entity.getGoodsSaledoList();
        // 如果订单请购下的订单全都有库存，则不生成请购单，直接修改产品状态为发货中，并锁定库存
        boolean allZero = true;
        for (GoodsSaledo goodsSaledo : rows) {
            if(goodsSaledo.getPleaseBuyApplyCount()!=null && goodsSaledo.getPleaseBuyApplyCount().compareTo(new BigDecimal(0))!=0 ){//请购数量 = 0  正数 负数都需要请购
                allZero= false;
            }
        }
        if(allZero){
            AjaxResult.error("请购数量都为0，请填写请购数量！");
        }
        goodsSaledoService.pleaseBuyForTwiceToTech(entity);
        return AjaxResult.success("提交完成！");
    }

    //库管 二次请购 提交到 采购部
    @PostMapping("/pleaseBuyForTwiceToBuy")
    public AjaxResult pleaseBuyForTwiceToCaigou(@Validated @RequestBody LvDo entity) {
        List<GoodsSaledo> rows = entity.getGoodsSaledoList();
        // 如果订单请购下的订单全都有库存，则不生成请购单，直接修改产品状态为发货中，并锁定库存
        boolean allZero = true;
        for (GoodsSaledo goodsSaledo : rows) {
            if(goodsSaledo.getPleaseBuyApplyCount()!=null && goodsSaledo.getPleaseBuyApplyCount().compareTo(new BigDecimal(0))!=0 ){//请购数量 = 0  正数 负数都需要请购
                allZero= false;
            }
        }
        if(allZero){
            AjaxResult.error("请购数量都为0，请填写请购数量！");
        }
        goodsSaledoService.pleaseBuyForTwiceToBuy(entity);
        return AjaxResult.success("提交完成！");
    }

    //自制产品设为不自制
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledo:query')")
    @DeleteMapping(value = "/isNotDo/{id}")
    public AjaxResult isNotDo(@PathVariable("id") String id) {
        return AjaxResult.success(goodsSaledoService.isNotDo(id));
    }

    //自制产品设为自制
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledo:query')")
    @DeleteMapping(value = "/isDo/{id}")
    public AjaxResult isDo(@PathVariable("id") String id) {
        return AjaxResult.success(goodsSaledoService.isDo(id));
    }

}
